<?php
//require_once '../libs/db.php';
//require_once '../classes/userManager.php';
require_once '../config/config.php';

$resetSuccess = false;
$useremail = '';
$submitted = ($_SERVER['REQUEST_METHOD'] == "POST");

if ($submitted) {
    $useremail = $_POST['useremail'];
    $userManager = new userManager();
    $resetSuccess = ($userManager->resetPassword($_POST['useremail']));
    if ($resetSuccess) {
        header("Location: resetpwd.php");
        exit;
    }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <?php include('common/headsection.php'); ?>
        <title>iBill - Forgot Password</title>
    </head>
    <body>
        <div id="page">
            <div id="wrapper">
                <?php include('common/header.php'); ?>
                <div id="forgotpass">
                    <form name="forgotpass" action="forgotpass.php" method="POST">
                        <fieldset style="width:25%">
                            <legend>Reset Password</legend>
                            <div><span class="error">
                                    <?php
                                    if ($submitted && !$resetSuccess) {
                                        echo "<span style='font-weight: bold'>Please resolve the below issues.</span><br/>";
                                        echo "<ul>";
                                        foreach ($userManager->errs as $err => $errmsg) {
                                            echo '<li>' . $errmsg;
                                        }
                                        echo "</ul><br/>";
                                    } else {
                                        echo "<br/>";
                                    }
                                    ?>
                                </span></div>
                            <label for id="lbluseremail">Email:</label>
                            <input type="text" name="useremail" value="<?php echo htmlentities($useremail); ?>">
                            <br/><br/>
                            <input type="Submit" value="Reset Password"><br/><br/>
                            <div>
                                <span class="index">Back to
                                    <a href="index.php">Login Page</a>
                                </span>
                            </div>
                        </fieldset>
                    </form>
                </div>
                <?php include('common/footer.php'); ?>
            </div>
        </div>
    </body>
</html>
